﻿using System;
using System.Collections.Generic;
using LogSystem;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class VehicleRacingManager : MonoBehaviour
    {
        public CarEntity playerVehicle;

        public Transform checkPointsRoot;

        [Header("如果车辆一直在远离目标点，超过一定时间还原位置")]
        public float vehicleRevertTime = 5;
        
        private float _farAwayTime;
        private float _lastCheckDistance;

        public LinkedList<VehicleCheckPoint> CheckPoints = new LinkedList<VehicleCheckPoint>();

        private LinkedListNode<VehicleCheckPoint> _currentCheckPoint; 

        private void Awake()
        {
            if (checkPointsRoot)
            {
                var ps= checkPointsRoot.GetComponentsInChildren<VehicleCheckPoint>(true);
                if (ps == null || ps.Length == 0)
                {
                    return;
                }
                
                for (int i = 0; i < ps.Length; i++)
                {
                    CheckPoints.AddLast(new LinkedListNode<VehicleCheckPoint>(ps[i]));
                    ps[i].transform.gameObject.SetActive(false);
                }

                _currentCheckPoint = CheckPoints.First;
                _currentCheckPoint.Value.gameObject.SetActive(true);
                // if (_currentCheckPoint.Next != null) 
                //     _currentCheckPoint.Next.Value.gameObject.SetActive(true);
            }
            else
            {
                Debug.LogError(LogModule.LibertyScene,"没有赛车检查点");
            }

            VehicleCheckPoint.OnPass += OnPassCheckPoint;

        }

        private void OnPassCheckPoint(VehicleCheckPoint checkPoint)
        {
            if (checkPoint == _currentCheckPoint.Value)
            {
                // 通过目标检查点

                
                if (_currentCheckPoint.Next == null)
                {
                    // 到达终点
                    _currentCheckPoint = null;
                    
                    StreetFinishRacingEvent.OnFinishRacing?.Invoke();
                }
                else
                {
                    _currentCheckPoint = _currentCheckPoint.Next;
                    _currentCheckPoint.Value.gameObject.SetActive(true);
                }
            }
        }


        // private void FixedUpdate()
        // {
        //     if (_currentCheckPoint != null && playerVehicle)
        //     {
        //         float limitDistance = 100;
        //         if (_currentCheckPoint.Previous!=null)
        //         {
        //             limitDistance = Vector3.Distance(_currentCheckPoint.Value.transform.position,
        //                 _currentCheckPoint.Previous.Value.transform.position) * 2f;
        //         }
        //         
        //         float d = Vector3.Distance(_currentCheckPoint.Value.transform.position, playerVehicle.position);
        //         if (d > _lastCheckDistance)
        //         {
        //             _farAwayTime += Time.fixedDeltaTime;
        //             _lastCheckDistance = d;
        //             
        //             if (_farAwayTime > vehicleRevertTime && d > limitDistance) // 路线错了
        //             {
        //                 RevertVehicle();
        //                 _farAwayTime = 0;
        //                 _lastCheckDistance = 0;
        //             }
        //         }
        //         else
        //         {
        //             _farAwayTime = 0;
        //             _lastCheckDistance = 0;
        //         }
        //     }    
        // }


        public void RevertVehicle()
        {
            // 回到上一个检查点   
            VehicleCheckPoint checkPoint = _currentCheckPoint.Value;
            if (_currentCheckPoint.Previous != null)
            {
                checkPoint = _currentCheckPoint.Previous.Value;
            }

            var rp = checkPoint.RevertPoint;

            playerVehicle.carController.immobilize();
            playerVehicle.carController.setPosition(rp.position);
            playerVehicle.carController.setRotation(rp.rotation);
            
            LibertyAIUtils.PlayerController.inputCamera.ResetCamera();
        }

        public void Dispose()
        {
            
            VehicleCheckPoint.OnPass -= OnPassCheckPoint;
        }
    }
}
